package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.QuestionsAndAnswers;
import cn.wolfcode.trip.base.domain.QuestionsContent;
import cn.wolfcode.trip.base.mapper.QuestionsAndAnswersMapper;
import cn.wolfcode.trip.base.mapper.QuestionsContentMapper;
import cn.wolfcode.trip.base.query.QuestionsAndAnswersQueryObject;
import cn.wolfcode.trip.base.service.IQuestionsAndAnswersService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionsAndAnswersServiceImpl implements IQuestionsAndAnswersService {
    @Autowired
    private QuestionsAndAnswersMapper questionsAndAnswersMapper;
    @Autowired
    private QuestionsContentMapper contentMapper;

    @Override
    public QuestionsAndAnswers getQuestionsAndAnswersById(Long id) {
        return questionsAndAnswersMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(QuestionsAndAnswers questionsAndAnswers) {
        //设置作者
        questionsAndAnswers.setAuthor(UserContext.getCurrentUser());
        questionsAndAnswersMapper.insert(questionsAndAnswers);

        //保存内容
        QuestionsContent content = questionsAndAnswers.getContent();
        //设置id
        content.setId(questionsAndAnswers.getId());
        contentMapper.insert(content);
        //设置关联外键
        questionsAndAnswersMapper.updateByPrimaryKey(content);
    }

    @Override
    public PageInfo<QuestionsAndAnswers> query(QuestionsAndAnswersQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<QuestionsAndAnswers> list = questionsAndAnswersMapper.selectForList(qo);
        return new PageInfo<>(list);
    }
    

}
